<?php
/**
 * Proxy class which add pointcuts to proxed object
 *
 * Last modified at 10/07/08
 * 
 * @author Yuri Tolochko <yuri.tolochko@gmail.com>
 * @package ytAOP
 * @version 0.0.3
 */
class AOPProxy {
	
	/**
	 * Proxed object
	 *
	 * @var object
	 */
	private $__proxedObject;
	
	/**
	 * Constructor of Proxy class
	 *
	 * @param mixed $object
	 * @param array $points
	 */
	public function __construct($object) {
		// set object
		if (is_object($object)) {
			$this->__proxedObject = $object;
		} elseif (is_string($object)) {
			if (class_exists($object)) {
				$this->__proxedObject = new $object;
			}
		}
	}
	
	/**
	 * Provide access to call proxed object methods
	 *
	 * @magic 
	 * @param string $method
	 * @param array $args
	 * @return mixed
	 */
	public function __call($method, $args) {
		
		// get class name of proxed object
		$className = get_class($this->__proxedObject);
		
		$AOPProvider = AOPProvider::getInstance();
		
		// call before method advices
		$AOPProvider->callAdvices($className, $method, AOPProvider::EVENT_BEFORE, $args);	
		
		$result = call_user_func_array(array($this->__proxedObject, $method), $args);
		
		// call after method advices
		$AOPProvider->callAdvices($className, $method, AOPProvider::EVENT_AFTER, $args);	
		
		return $result;
	}
	
	/**
	 * Provide access to getting value of proxed object field
	 *
	 * @magic 
	 * @param string $fieldName
	 * @return mixed
	 */
	public function __get($fieldName) {
		return $this->__proxedObject->$fieldName;
	}
	
	/**
	 * Provide access to setting value to proxed object field
	 *
	 * @magic 
	 * @param string $fieldName
	 * @param mixed $value
	 */
	public function __set($fieldName, $value) {
		$this->__proxedObject->$fieldName = $value;
	}

}